package com.hmeter.aws.s3;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;

public class S3Sampler extends AbstractSampler {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1851043746828088961L;

	private static final String AWSCREDENTIALSFILEPATH = "AWSCREDENTIALSFILEPATH";
	private static final String BUCKETNAME = "BUCKETNAME";
	private static final String KEY = "KEY";
	private static final String REGION = "REGION";
	private static final String INPUTFILEPATH = "INPUTFILEPATH";
	private static final String OUTPUTFILEPATH = "OUTPUTFILEPATH";
	private static final String CREATENEWBUCKETBOOL = "CREATENEWBUCKETBOOL";
	private static final String UPLOADFILETOBUCKETBOOL = "UPLOADFILETOBUCKETBOOL";
	private static final String READFILEFROMBUCKETBOOL = "READFILEFROMBUCKETBOOL";
	private static final String DOWNLOADFILEFROMBUCKETBOOL = "DOWNLOADFILEFROMBUCKETBOOL";
	private static final String GETLISTOFOBJECTSINBUCKETBOOL = "GETLISTOFOBJECTSINBUCKETBOOL";
	private static final String DELETEOBJECTFROMBUCKETBOOL = "DELETEOBJECTFROMBUCKETBOOL";
	private static final String DELETEBUCKETBOOL = "DELETEBUCKETBOOL";
	
	@Override
	public SampleResult sample(Entry e) {
		SampleResult result = new SampleResult();
		
		String requestData = "bucket name: " + this.getBucketName() + "\n";
		requestData += "key: " + this.getKey() + "\n";
		requestData += "region: " + this.getRegion() + "\n";
		
		result.setSampleLabel(getName());
		result.setSamplerData(requestData);
		result.setDataType(SampleResult.TEXT);

		result.sampleStart();
		
		S3SamplerUtility s3SamplerUtility = null;
		
		try {
			s3SamplerUtility = new S3SamplerUtility(this.getAwsCredentialsFilePath());
		} catch (FileNotFoundException e1) {
			result.setResponseData(e1.getMessage().getBytes());
			result.setSuccessful(false);
		} catch (IllegalArgumentException e1) {
			result.setResponseData(e1.getMessage().getBytes());
			result.setSuccessful(false);
		} catch (IOException e1) {
			result.setResponseData(e1.getMessage().getBytes());
			result.setSuccessful(false);
		}
		
		if (this.isCreateNewBucketBool())
		{
			String response = s3SamplerUtility.createNewBucket(this.getBucketName(), this.getRegion());
			result.setResponseData(response.getBytes());
			result.setSuccessful(true);
		}
		
		if (this.isUploadFileToBucketBool())
		{
			String response = s3SamplerUtility.uploadFileToBucket(this.getBucketName(), this.getKey(), this.getInputFilePath());
			result.setResponseData(response.getBytes());
			result.setSuccessful(true);
		}
		
		if (this.isReadFileFromBucketBool())
		{
			String response = "";
			try {
				response = s3SamplerUtility.readFileFromBucket(this.getBucketName(), this.getKey());
				result.setResponseData(response.getBytes());
				result.setSuccessful(true);
			} catch (IOException e1) {
				result.setResponseData(response.getBytes());
				result.setSuccessful(false);
			}
		}
		
		if (this.isDownloadFileFromBucketBool())
		{
			String response = s3SamplerUtility.downloadFileFromBucket(this.getBucketName(), this.getKey(), this.getOutputFilePath());
			result.setResponseData(response.getBytes());
			result.setSuccessful(true);
		}
		
		if (this.isGetListOfObjectsInBucketBool())
		{
			String response = s3SamplerUtility.downloadFileFromBucket(this.getBucketName(), this.getKey(), this.getOutputFilePath());
			result.setResponseData(response.getBytes());
			result.setSuccessful(true);
		}
		
		if (this.isDeleteObjectFromBucketBool())
		{
			String response = s3SamplerUtility.deleteObjectFromBucket(this.getBucketName(), this.getKey());
			result.setResponseData(response.getBytes());
			result.setSuccessful(true);
		}
		
		if (this.isDeleteBucketBool())
		{
			String response = s3SamplerUtility.deleteBucket(this.getBucketName());
			result.setResponseData(response.getBytes());
			result.setSuccessful(true);
		}
		
		result.sampleEnd();
		return result;
	}
	
	public void setAwsCredentialsFilePath(String awsCredentialsFilePath)
	{
		setProperty(AWSCREDENTIALSFILEPATH, awsCredentialsFilePath);
	}
	
	public String getAwsCredentialsFilePath()
	{
		return getPropertyAsString(AWSCREDENTIALSFILEPATH);
	}
	
	public void setBucketName(String bucketName)
	{
		setProperty(BUCKETNAME, bucketName);
	}
	
	public String getBucketName()
	{
		return getPropertyAsString(BUCKETNAME);
	}
	
	public void setKey(String key)
	{
		setProperty(KEY, key);
	}
	
	public String getKey()
	{
		return getPropertyAsString(KEY);
	}
	
	public void setRegion(String region)
	{
		setProperty(REGION, region);
	}
	
	public String getRegion()
	{
		return getPropertyAsString(REGION);
	}
	
	public void setInputFilePath(String inputFilePath)
	{
		setProperty(INPUTFILEPATH, inputFilePath);
	}
	
	public String getInputFilePath()
	{
		return getPropertyAsString(INPUTFILEPATH);
	}
	
	public void setOutputFilePath(String outputFilePath)
	{
		setProperty(OUTPUTFILEPATH, outputFilePath);
	}
	
	public String getOutputFilePath()
	{
		return getPropertyAsString(OUTPUTFILEPATH);
	}
	
	public boolean isCreateNewBucketBool()
	{
		return getPropertyAsBoolean(CREATENEWBUCKETBOOL);
	}
	
	public void setCreateNewBucketBool(boolean createNewBucketBool)
	{
		setProperty(CREATENEWBUCKETBOOL, createNewBucketBool);
	}
	
	public boolean isUploadFileToBucketBool()
	{
		return getPropertyAsBoolean(UPLOADFILETOBUCKETBOOL);
	}
	
	public void setUploadFileToBucketBool(boolean uploadFileToBucketBool)
	{
		setProperty(UPLOADFILETOBUCKETBOOL, uploadFileToBucketBool);
	}
	
	public boolean isReadFileFromBucketBool()
	{
		return getPropertyAsBoolean(READFILEFROMBUCKETBOOL);
	}
	
	public void setReadFileFromBucketBool(boolean readFileFromBucketBool)
	{
		setProperty(READFILEFROMBUCKETBOOL, readFileFromBucketBool);
	}
	
	public boolean isDownloadFileFromBucketBool()
	{
		return getPropertyAsBoolean(DOWNLOADFILEFROMBUCKETBOOL);
	}
	
	public void setDownloadFileFromBucketBool(boolean downloadFileFromBucketBool)
	{
		setProperty(DOWNLOADFILEFROMBUCKETBOOL, downloadFileFromBucketBool);
	}
	
	public boolean isGetListOfObjectsInBucketBool()
	{
		return getPropertyAsBoolean(GETLISTOFOBJECTSINBUCKETBOOL);
	}
	
	public void setGetListOfObjectsInBucketBool(boolean getListOfObjectsInBucketBool)
	{
		setProperty(GETLISTOFOBJECTSINBUCKETBOOL, getListOfObjectsInBucketBool);
	}
	
	public boolean isDeleteObjectFromBucketBool()
	{
		return getPropertyAsBoolean(DELETEOBJECTFROMBUCKETBOOL);
	}
	
	public void setDeleteObjectFromBucketBool(boolean deleteObjectFromBucketBool)
	{
		setProperty(DELETEOBJECTFROMBUCKETBOOL, deleteObjectFromBucketBool);
	}
	
	public boolean isDeleteBucketBool()
	{
		return getPropertyAsBoolean(DELETEBUCKETBOOL);
	}
	
	public void setDeleteBucketBool(boolean deleteBucketBool)
	{
		setProperty(DELETEBUCKETBOOL, deleteBucketBool);
	}
}
